import {Restivus} from 'meteor/nimble:restivus';
import {Meteor} from 'meteor/meteor';
import { Random } from 'meteor/random';
import {DeviceAlarms} from '../imports/api/devicealarms';
if (Meteor.isServer) {
    Meteor.startup(() => {
        let Api = new Restivus({
            useDefaultAuth: true,
            prettyJson: true,
        });
       
        //警告操作记录列表
        Api.addRoute('getDeviceAlarms', {authRequired: false}, {
            post: {
                action() {
                    if (this.bodyParams) {
                        const param = this.bodyParams;
                        const logintoken = param.token;
                        const familyId = param.familyId;
                        const user = Meteor.users.findOne({"services.resume.loginTokens.token":logintoken});
                        const selector = {telephone: user.username};
                        if(familyId != ''){
                            selector['familyId'] = familyId;
                        }
                        if(user) {
                            const logs = DeviceAlarms.find(selector).fetch() || [];
                            return {success: true, result: logs, resultCode: 1000}
                        }else{
                            return {success: false, result: [], resultCode: 1004};
                        }
                    }
                    return {success: false, result: [], resultCode: 1002};
                }
            }
        });
    });

}